#ifndef __SOFT_I2C_PLAT_H
#define __SOFT_I2C_PLAT_H

#include <stdint.h>
#include <stdbool.h>
#include "soft_i2c.h"
#include "gpio.h"
#include "tim.h"

void stm32_soft_i2c_gpio_set_direction(uint32_t pin, soft_i2c_gpio_direction direction)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	
	GPIO_InitStruct.Pin = pin;
	GPIO_InitStruct.Mode = direction ? GPIO_MODE_OUTPUT_PP : GPIO_MODE_INPUT;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
	HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

void stm32_soft_i2c_gpio_set_level(uint32_t pin, soft_i2c_gpio_level level)
{
	HAL_GPIO_WritePin(GPIOB, pin, (GPIO_PinState)level);
}

soft_i2c_gpio_level stm32_soft_i2c_gpio_get_level(uint32_t pin)
{
	return (soft_i2c_gpio_level)HAL_GPIO_ReadPin(GPIOB, pin);
}

void stm32_soft_i2c_delay_us(uint32_t us)
{
	#if 0 
	for (int i = 0; i < us; i++) {
		__HAL_TIM_SetCounter(&htim2, 0);
		__HAL_TIM_ENABLE(&htim2);
		
		while (__HAL_TIM_GetCounter(&htim2) < 10);
		
		__HAL_TIM_DISABLE(&htim2);
	}
	#endif
	for (int i = 0; i < us; i++) {
		__NOP();
	};
}

#endif